package design.singleton;

/**
 * @Description: 作用描述
 * @Author: hmm
 * @CreateDate: 2019/7/24$ 11:40$
 * @UpdateUser: hmm
 * @UpdateDate: 2019/7/24$ 11:40$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class LazySingleton {
    /**初始时 没有实例化*/
    private static volatile LazySingleton singleton;

    /**构造私有，无法被他人new*/
    private LazySingleton () {}

    /**第一种：如果没有实例才new*/

    /**
     * public static LazySingleton getInstance() {
        if (design.singleton == null) {
            design.singleton = new LazySingleton();
        }
        return design.singleton;
    }*/

    /**第二种：考虑多线程*/
    /**
     * public static synchronized LazySingleton getInstance() {
        if (design.singleton == null) {
            design.singleton = new LazySingleton();
        }
        return design.singleton;
    }*/

    /**
     * 第三种 dcl + volitaile
     * 加volitaile的原因：new对象并赋值会出现重排序
     *
     * */
    public static LazySingleton getInstance() {
        if (singleton == null) {
            synchronized (LazySingleton.class) {
                if (singleton == null) {
                    singleton = new LazySingleton();
                }
            }
        }
        return singleton;
    }

}
